package cn.gx.learn.security_boot_jwt_websocket.websocket_origin.decoder;

import cn.gx.learn.security_boot_jwt_websocket.websocket_origin.dal.database.Message;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.websocket.DecodeException;
import jakarta.websocket.Decoder;

public class MessageDecoder implements Decoder.Text<Message> {

    @Override
    public Message decode(String s) throws DecodeException {
        Message message = JSON.parseObject(s,Message.class);
        return message;
    }

    @Override
    public boolean willDecode(String s) {
        try {
            JSONObject obj = JSON.parseObject(s);
            return obj != null
                    && obj.containsKey("message");
        } catch (Exception e) {
            return false;
        }
    }
}
